package com.springboot.blog.service.impl;

import com.springboot.blog.entity.Nav;
import com.springboot.blog.dao.NavDao;
import com.springboot.blog.service.NavService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 4vsy
 * @date 2020-05-11 16:13
 **/
@Service
public class NavServiceImpl implements NavService {
    @Resource
    private NavDao navDao;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(Nav record) {
        return 0;
    }

    @Override
    public Nav selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public List<Nav> selectAll() {
        List<Nav> navs = navDao.selectAll();
        return unlimitedTree(navs, 0);
    }

    @Override
    public int updateByPrimaryKey(Nav record) {
        return 0;
    }

    public static List<Nav> formatNavs(List<Nav> navs,List<Nav> navs_list,Integer pid) {
        for (Nav nav:navs){
            navs_list.add(nav);
            List<Nav> childrenNavs=nav.getChildren();
            if(null!=childrenNavs){
                navs_list=formatNavs(childrenNavs,navs_list,nav.getPid());
            }
        }
        return navs_list;
    }
    public static List<Nav> unlimitedTree(List<Nav> navs,Integer pid) {
        ArrayList<Nav> navs_list =new ArrayList<>();
        for (Nav nav:navs){
            if(pid==nav.getPid()){
                nav.setChildren(unlimitedTree(navs,nav.getId()));
                navs_list.add(nav);
            }
        }
        return navs_list;
    }

}
